package com.dmai.capacity.platform.user.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.hutool.db.DaoTemplate;
import com.alibaba.fastjson.JSON;
import com.dmai.capacity.platform.common.dto.ResponseEntity;
import com.dmai.capacity.platform.common.model.Capability;
import com.dmai.capacity.platform.common.utils.DateUtils;
import com.dmai.capacity.platform.common.utils.StringUtil;
import com.dmai.capacity.platform.log.config.AuditLogStreamsConfig;
import com.dmai.capacity.platform.oauth.common.service.ICapabilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.config.BindingServiceConfiguration;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("capabilitys")
@ConditionalOnClass(BindingServiceConfiguration.class)
@AutoConfigureBefore({BindingServiceConfiguration.class})
@EnableBinding(AuditLogStreamsConfig.Source.class)
public class CapabilityController {

    @Autowired
    private ICapabilityService capabilityService;
    @Autowired
    private AuditLogStreamsConfig.Source source;


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @PreAuthorize("hasAnyAuthority('capability:capability:list')")
    public ResponseEntity list(@RequestParam Map<String, Object> params, HttpServletRequest request) throws InterruptedException {
        List<Capability> all = capabilityService.findAll(params);
        CalligraphyHandleResult ss = new CalligraphyHandleResult();

        String traceId = request.getHeader("x-sessionId-header") == null ? "66051460cc2ac69cd8438b66" : request.getHeader("x-sessionId-header").toString();
        ss.setSessionId(traceId);
        ss.setCode(new Random().nextInt(2) - 1);


         boolean a = source.output().send(MessageBuilder.withPayload(JSON.toJSON(ss))
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build());

         if(a) {
             System.out.println("-----OK:" + ss.getSessionId());
         } else {
             System.out.println("-----fail:" + ss.getSessionId());
         }

        return ResponseEntity.succeed(all);
    }


}

